const {cartFormatError, invalidGoodsId} = require('../constant/err.type')
const goods = require('../model/goods.model')
const validator = (rules)=>{
    return async (ctx,next)=>{

        try {
            ctx.verifyParams(rules)
        } catch (error) {
            console.log(error);
            cartFormatError.result = error
            return ctx.app.emit('error',cartFormatError,ctx)
        }
        await next()
    }
}
const goodsIfExist = async(ctx,next)=>{
    try {
        //判断商品是否存在
        const {goods_id} = ctx.request.body
        const res = await goods.findOne({
            where:{
                id:goods_id
            }
        })
        console.log(res,'11111');
        
        if(res){
            //找到了这件商品
           await next()
        }else{
            invalidGoodsId.result = '添加的商品不存在'
            return ctx.app.emit('error',invalidGoodsId,ctx)
        }
    } catch (error) {
        console.log(error);
    }
}

module.exports = {
    validator,
    goodsIfExist
}